package team.archai.nexus.flow.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

/**
 * @author lishibin
 * @desc: 流程设计器资源文件注册配置类
 * @date 2023-08-03
 */
@Configuration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@ConditionalOnProperty(value = "archai-nexus.flow-designer.enabled", havingValue = "true")
public class FlowDesignerWebConfiguration {

    @Value("${archai-nexus.flow-designer.base-url:}")
    private String baseUrl;

    @Bean
    public FlowDesignerWebConfigurerAdapter configurer() {
        return new FlowDesignerWebConfigurerAdapter(fixup(baseUrl));
    }

    private String fixup(String swaggerBaseUrl) {
        return StringUtils.trimTrailingCharacter(nullToEmpty(swaggerBaseUrl), '/');
    }

    private String nullToEmpty(String str) {
        return null == str ? "" : str;
    }
}
